﻿namespace TokenProbability

open System
open TokenReader

// Computes token probability table
module TokenProbability =
    // Samples have 15 emails in each inbox and spam CSV file
    // TODO: this should not be constant and should be obtained from CSV files
    let private mailsCount = 15m
    
    // File with good emails
    [<Literal>]
    let private inboxCSV = "..\..\Samples\inbox.csv"
    
    // File with bad emails
    [<Literal>]
    let private spamCSV = "..\..\Samples\spam.csv"
   
    // Computes probability that a token is spam based on number of token occurences in good and bad emails
    let private GetTokenProbability(good, bad) = 
        let pBad = Math.Min(1m, decimal(bad) / mailsCount)
        let pGood = Math.Min(1m, 2m * decimal(good) / mailsCount)
        let P = (pBad / (pGood + pBad))
        Math.Max(0.01m, Math.Min(0.99m, P))
        
    // Computes token probability table
    let GetProbabilityTable() =
        let inbox = TokenReader.GetWordDensityMap(inboxCSV)
        let spam = TokenReader.GetWordDensityMap(spamCSV)

        let keys = inbox.Keys |> Seq.append spam.Keys |> Seq.distinct |> Seq.toList
        dict<string, decimal>(seq {
            for token in keys do
                let good = if inbox.ContainsKey(token) then inbox.[token] else 0
                let bad = if spam.ContainsKey(token) then spam.[token] else 0
                yield token, GetTokenProbability(good, bad)
        })

    

